package com.party.mobile.web.controller.member;

import com.party.authorization.annotation.Authorization;
import com.party.common.paging.Page;
import com.party.common.utils.PartyCode;
import com.party.common.utils.StringUtils;
import com.party.core.model.YesNoStatus;
import com.party.core.model.member.Member;
import com.party.core.model.member.MemberGroup;
import com.party.core.model.message.IdentityType;
import com.party.core.model.message.Message;
import com.party.core.model.message.MessageType;
import com.party.core.model.notify.NotifySysMessage;
import com.party.core.service.member.IMemberGroupService;
import com.party.core.service.message.IMessageService;
import com.party.core.service.notify.INotifySysMessageService;
import com.party.mobile.biz.currentUser.CurrentUserBizService;
import com.party.mobile.web.dto.AjaxResult;
import org.apache.commons.lang3.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 合作商消息控制器
 * Created by wei.li
 *
 * @date 2017/9/8 0008
 * @time 14:51
 */

@Controller
@RequestMapping(value = "officialPartner/message")
public class OfficialPartnerMessageController {

    @Autowired
    private IMessageService messageService;

    @Autowired
    private CurrentUserBizService currentUserBizService;

    @Autowired
    private INotifySysMessageService notifySysMessageService;

    @Autowired
    private IMemberGroupService memberGroupService;

    Logger logger = LoggerFactory.getLogger(getClass());


    /**
     * 消息列表
     *
     * @param message 消息
     * @param page    分页参数
     * @param request 请求参数
     * @return 交互数据
     */
    @ResponseBody
    @Authorization
    @RequestMapping(value = "list")
    public AjaxResult list(Message message, Page page, HttpServletRequest request) {
        String memberId = currentUserBizService.getCurrentUser(request).getId();
        message.setCreateBy(memberId);
        message.setIdentityType(IdentityType.OFFICIAL_PARTNER.getCode());
        message.setExcludeType(MessageType.IMPORTANT_NOTIFY.getCode());
        List<Message> list = messageService.listPage(message, page);
        return AjaxResult.success(list, page);
    }


    /**
     * 初始化
     *
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "init")
    public AjaxResult init() {
        List<Message> list = messageService.list(new Message());
        for (Message message : list) {
            message.setIdentityType(IdentityType.NORMAL.getCode());
            messageService.update(message);
        }
        return AjaxResult.success();
    }

    /**
     * 消息详情
     *
     * @param msgId
     * @return
     */
    @ResponseBody
    @RequestMapping("getDetails")
    public AjaxResult getDetails(String msgId) {
        if (StringUtils.isEmpty(msgId)) {
            return AjaxResult.error(PartyCode.PARAMETER_ILLEGAL, "id不能为空");
        }

        Message message = messageService.get(msgId);
        return AjaxResult.success(message);
    }

    /**
     * 重要通知列表
     *
     * @param request
     * @param page
     * @return
     */
    @ResponseBody
    @RequestMapping("getImportantNotifyList")
    public AjaxResult getImportantNotifyList(HttpServletRequest request, Page page) {
        try {
            String memberId = currentUserBizService.getCurrentUser(request).getId();
            String partnerId = memberGroupService.getPartnerId(memberId);
            Message message = new Message();
            message.setCreateBy(partnerId);
            message.setIdentityType(IdentityType.OFFICIAL_PARTNER.getCode());
            message.setMessageType(MessageType.IMPORTANT_NOTIFY.getCode());
            List<Message> list = messageService.listPage(message, page);
            for (Message mm : list) {
                mm.setContent(null);
            }
            return AjaxResult.success(list, page);
        } catch (Exception e) {
            return AjaxResult.error(100, "重要通知消息列表");
        }
    }

    /**
     * 重要通知详情
     *
     * @param id           id
     * @param sysMessageId 系统消息id
     * @return
     */
    @ResponseBody
    @RequestMapping("getImportantNotifyDetail")
    public AjaxResult getImportantNotifyDetail(String id, String sysMessageId) {
        if (StringUtils.isEmpty(id) && StringUtils.isEmpty(sysMessageId)) {
            return AjaxResult.error(PartyCode.PARAMETER_ILLEGAL, "id不能为空");
        }
        try {
            if (StringUtils.isNotEmpty(id)) {
                Message message = messageService.get(id);
                if (message != null) {
                    NotifySysMessage sysMessage = notifySysMessageService.get(message.getRelId());
                    if (sysMessage != null) {
                        if (StringUtils.isNotEmpty(sysMessage.getContent())) {
                            String content = StringEscapeUtils.unescapeHtml4(sysMessage.getContent());
                            message.setContent(content);
                        }
                    }
                    return AjaxResult.success(message);
                }
            }

            if (StringUtils.isNotEmpty(sysMessageId)) {
                Message message = new Message();
                NotifySysMessage sysMessage = notifySysMessageService.get(sysMessageId);
                if (sysMessage != null) {
                    if (StringUtils.isNotEmpty(sysMessage.getContent())) {
                        String content = StringEscapeUtils.unescapeHtml4(sysMessage.getContent());
                        message.setContent(content);
                    }
                    message.setTitle(sysMessage.getTitle());
                    message.setCreateDate(sysMessage.getSendTime());
                }
                return AjaxResult.success(message);
            }
            return AjaxResult.error(100, "详情不存在");
        } catch (Exception e) {
            logger.error("获取重要通知详情异常", e);
            return AjaxResult.error(100, "获取重要通知详情异常");
        }
    }

    /**
     * 获取重要通知未读数字异常
     *
     * @param request
     * @return
     */
    @ResponseBody
    @Authorization
    @RequestMapping("getImportantNotifyCount")
    public AjaxResult getImportantNotifyCount(HttpServletRequest request) {
        try {
            String memberId = currentUserBizService.getCurrentUser(request).getId();
            Message message = new Message();
            message.setCreateBy(memberId);
            message.setIsNew(YesNoStatus.YES.getCode());
            message.setMessageType(MessageType.IMPORTANT_NOTIFY.getCode());
            message.setIdentityType(IdentityType.OFFICIAL_PARTNER.getCode());
            Integer count = messageService.getCount(message);
            return AjaxResult.success(count);
        } catch (Exception e) {
            logger.error("获取重要通知未读数字异常", e);
            return AjaxResult.error(100, "获取重要通知未读数字异常");
        }
    }
}
